/**
 * @param {number[]} ratings
 * @return {number}
 */
var candy = function (ratings) {
  let candy = new Array(ratings.length);
  candy[0] = 1;
  for (let i = 1; i < ratings.length; i++) {
    // 递增
    candy[i] = ratings[i] > ratings[i - 1] ? candy[i - 1] + 1 : 1;
  }
  let sum = candy[ratings.length - 1];
  for (let i = ratings.length - 2; i >= 0; i--) {
    // 递减
    if (ratings[i] > ratings[i + 1])
      candy[i] = Math.max(candy[i], candy[i + 1] + 1);
    sum += candy[i];
  }
  return sum;
};
let ratings = [1, 0, 2];
console.log(candy(ratings));
ratings = [1, 2, 2];
console.log(candy(ratings));
ratings = [1, 2, 87, 87, 87, 2, 1];
console.log(candy(ratings));
ratings = [1, 3, 2, 2, 1];
console.log(candy(ratings));
